﻿@{
	Layout = "~/Views/Shared/_Layout.cshtml";
}

@using dPet.Application.DataObjects;
@using dPet.Application.QueryObjects;
@using Hangerd.Mvc.Extensions;
@using Hangerd.Utility;

@model Hangerd.Mvc.ViewModels.PagedListModel<SaleOrderDto>

<div ng-controller="SaleOrderController">
	<div class="page-header">
		<h3>销售订单记录</h3>
	</div>

	<div class="panel panel-default">
		<div class="panel-heading">
			@using (Html.BeginForm("List", "SaleOrder", FormMethod.Get, new { @class = "form-inline" }))
			{
				<div class="input-group">
					<span class="input-group-addon">订单号</span>
					<input type="text" name="OrderNumber" class="form-control input-sm" placeholder="请输入订单号" value="@ViewBag.QueryDto.OrderNumber" />
					<span class="input-group-addon">状态</span>
					<select name="Status" class="form-control input-sm">
						<option value="">全部</option>
						<option value="@OrderStatusDto.Init" @Html.IsSelected(((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Init)>未完成</option>
						<option value="@OrderStatusDto.Completed" @Html.IsSelected(((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Completed)>已完成</option>
						<option value="@OrderStatusDto.Canceled" @Html.IsSelected(((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Canceled)>已取消</option>
					</select>
					<span class="input-group-btn">
						<button id="btn_search" class="btn btn-info btn-sm" type="submit"><span class="glyphicon glyphicon-search"></span> 查 询</button>
					</span>
				</div>

				<a href="@Url.Action("List", "SaleOrder")" class="pull-right">清空筛选条件</a>
			}
		</div>

		<table class="table table-striped text-center">
			<thead>
				<tr>
					<th class="text-center">订单号</th>
					<th class="text-center">客户类别</th>
					<th class="text-center">总价（元）</th>
					<th class="text-center">下单时间</th>
					@if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Canceled)
					{
						<th class="text-center">取消时间</th>
					}
					else if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Completed)
					{
						<th class="text-center">完成时间</th>
						<th class="text-center">支付方式</th>
					}
					<th class="text-center">操作</th>
				</tr>
			</thead>
			<tbody>
				@foreach (var order in Model.List)
				{
					<tr>
						<td>@order.OrderNumber</td>
						<td>@CommonTools.GetEnumDescription(order.MembershipLevel)</td>
						<td>@order.TotalAmount</td>
						<td>@order.CreateTime.ToString("yyyy-MM-dd HH:mm:ss")</td>

						@if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Canceled)
						{
							<td>@order.CompleteTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
						}
						else if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Completed)
						{
							<td>@order.CompleteTime.ToString("yyyy-MM-dd HH:mm:ss")</td>
							<td>@CommonTools.GetEnumDescription(order.PaymentMethod)</td>
						}

						<td>
							@if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Init)
							{
								<a href="@Url.Action("Pay", "SaleOrder", new { id = order.Id })">处理订单</a>
							}
							else if (((SaleOrderQueryDto)ViewBag.QueryDto).Status == OrderStatusDto.Completed)
							{
								<a href="javascript:;" ng-click="printBill('@order.Id')">打印票据</a>
								<span>&nbsp;&nbsp;</span>
								<a href="javascript:;" ng-click="showSaleOrderDetails('@order.Id');">查看明细</a>
							}
							else
							{
								<a href="javascript:;" ng-click="showSaleOrderDetails('@order.Id');">查看明细</a>
							}
						</td>
					</tr>
				}
			</tbody>
		</table>
	</div>
</div>

<script type="text/ng-template" id="saleOrderDetailsModal">
	<div class="modal-header">
		<button type="button" class="close" ng-click="close();">&times;</button>
		<h4 class="modal-title">订单详情 [ 订单号: {{ mOrderNumber }} ]</h4>
	</div>
	<div class="modal-body form-horizontal">
		<table class="table table-striped text-center">
			<thead>
				<tr>
					<th class="text-center">商品条码</th>
					<th class="text-center">商品名称</th>
					<th class="text-center">商品单价</th>
					<th class="text-center">购买数量</th>
				</tr>
			</thead>
			<tbody>
				<tr ng-repeat='orderLine in mSaleOrderDetails'>
					<td>{{ orderLine.ProductBarcode }}</td>
					<td>{{ orderLine.ProductName }}</td>
					<td>{{ orderLine.UnitPrice | currency : '' }}</td>
					<td>{{ orderLine.Quantity }} {{ orderLine.UnitName }}</td>
				</tr>
			</tbody>
		</table>
	</div>
</script>

@HtmlUtil.BootstrapPager(Model.PageIndex, Model.PageSize, Model.TotalNumber,
	p => Url.Action("List", "SaleOrder", new 
	{
		OrderNumber = ViewBag.QueryDto.OrderNumber, 
		Status = ViewBag.QueryDto.Status, 
		pageIndex = p 
	}))

@section scripts {
	<script type="text/javascript">
		$(document).ready(function () {
			set_left_nav('#ln_SaleOrderList');
		});

		angular.module('dpet').controller('SaleOrderController', function ($scope, $http, $modal) {
			$scope.showSaleOrderDetails = function (orderId) {
				$http.post('/Ajax/SaleOrderDetails',
					{
						'orderId': orderId
					})
					.success(function (data) {
						var modalInstance = $modal.open({
							templateUrl: 'saleOrderDetailsModal',
							controller: function ($scope, $modalInstance, saleOrderDetails) {
								$scope.mSaleOrderDetails = saleOrderDetails;
								$scope.mOrderNumber = saleOrderDetails[0].OrderNumber;
								$scope.close = function () {
									$modalInstance.close();
								}
							},
							size: 'lg',
							resolve: {
								saleOrderDetails: function () {
									return data;
								}
							}
						});
					})
					.error(function (result) {
						alert("系统繁忙");
					})
			}

			$scope.printBill = function (orderId) {
				var url = "/SaleOrder/Bill/" + orderId;

				window.open(url, "_blank", 'width=600,height=500');
			}
		});
	</script>
}